package org.nokatag.tagjava;

import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.noka.gson.Gson;
import org.nokatag.dbgrid.GridBoxItem;
import org.nokatag.system.BugInfoWrint;
/**
 * <p>@Title 弹出编辑框</p>
 * <p>@Description </p>
 * <p>@Version 1.0.0</p>
 * <p>@author rebin</p>
 * <p>@date 2016年10月27日</p>
 * <p>xiefangjian@163.com</p>
 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
 */
public class NHTMLBox extends NokaTag{
	private static final long serialVersionUID = -3080167434316445829L;
	
	private String width = null;//宽
	private String height = null;//高
	private String id = null;//控件ID
	private String title = null;//标题
	private String onclose = null;//在关闭时调用的方法
	private String closebutton = null;//显示关闭按钮
	private String stopclose = null;//禁用点击外部空白处关闭弹框
	private Set<GridBoxItem> GridIds = null;
	
	
	public int doStartTag() throws JspException {
		JspWriter jspOut = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		GridIds = new LinkedHashSet<GridBoxItem>();//Grid
		StringBuilder bodystr = new StringBuilder();
		bodystr.append(potjs(request));
		bodystr.append(nhtmlboxjs(request));
		bodystr.append(nkBoxJsAndCss(request));
		bodystr.append("<div id=\""+id+"\" style=\"overflow: auto;display: none;\">");
		bodystr.append("<div class=\"tmask\" id=\""+id+"_boxid_tmask\" style=\"opacity: 0.7; display: block; height:100%; width:100%;z-index:999999\"></div>");
		bodystr.append("<div class=\"tbox\" id=\""+id+"_boxid_tbox\" style=\"display: none;position: absolute; top:0px; left:0px; opacity:1;display: block;z-index:9999999\">");
		bodystr.append("<div class=\"tinner\" id=\""+id+"_boxid_tinner\" style=\"background-image: none; height:"+LenNumber(height)+"px; width:"+LenNumber(width)+"px;\">");
		bodystr.append("<div class=\"tcontent\" id=\""+id+"_boxid_tcontent\">");
		bodystr.append("<div class=\"tiny_title\" style=\""+(null==title?"display:none":"")+"\" id=\""+id+"_boxid_tite\">"+(null==title?"":title)+"</div>");
		bodystr.append("<div class=\"\" id=\""+id+"_boxid_body\">");
		try {
			jspOut.write(bodystr.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	public int doEndTag() throws JspException {
		try {
			StringBuilder bodystr = new StringBuilder();
			String dst = "";
			if("false".equals(closebutton)){
				dst = "style=\"display: none;\"";
			}
			bodystr.append("</div><div  id=\""+id+"_boxid_tclose\" "+dst+" class=\"tclose\"></div>");
			bodystr.append("</div></div></div></div>");
			bodystr.append("<script type=\"text/javascript\">");
			bodystr.append("Event.observe(document, 'dom:loaded', function() {");
			bodystr.append("var nk_"+getId()+"_option ={");
			bodystr.append("id : '"+id+"',");
			bodystr.append("stopclose:"+(null==stopclose?"false":stopclose)+",");
			bodystr.append("width : "+LenNumber(width)+",");
			bodystr.append("height : "+LenNumber(height)+",");
			if(!GridIds.isEmpty()){
				try{
					Gson gson = new Gson();
					bodystr.append("gridids:"+gson.toJson(GridIds)+",");
				}catch(Exception se){}
			}
			if(null!=title){
				bodystr.append("title : '"+title+"',");
			}
			if(null!=onclose){
				bodystr.append("onclose : function(a){ "+onclose+"(a); }");
			}
			bodystr.append("};");
			bodystr.append("var nk_"+getId()+"_nokahtmlbox = new nokahtmlbox(nk_"+getId()+"_option);");
			bodystr.append("nk_"+getId()+"_nokahtmlbox.show();");
			//--------------infobox------------------------------------------------------------------
			bodystr.append("});");
			bodystr.append("</script>");
			bodystr.append("<div style=\"clear:both;\"></div>");
			pageContext.getOut().write(bodystr.toString());
		} catch (IOException e) {
				BugInfoWrint.Print(e);
		}
		return EVAL_PAGE;
	}
	public void addGrid(GridBoxItem grid){
		GridIds.add(grid);
	}
	public String getWidth() {
		return width;
	}
	public void setWidth(String width) {
		this.width = width;
	}
	public String getHeight() {
		return height;
	}
	public void setHeight(String height) {
		this.height = height;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getOnclose() {
		return onclose;
	}
	public void setOnclose(String onclose) {
		this.onclose = onclose;
	}
	public String getClosebutton() {
		return closebutton;
	}
	public void setClosebutton(String closebutton) {
		this.closebutton = closebutton;
	}
	public String getStopclose() {
		return stopclose;
	}
	public void setStopclose(String stopclose) {
		this.stopclose = stopclose;
	}
	
	

}
